<template>
	<div>
		<el-row :gutter="20">
			<el-col :span="8">
				<!-- 用户信息展示 -->
				<el-card shadow="hover" class="mgb20" style="height: 252px">
					<div class="user-info">
						<el-avatar :size="120" v-if="people.$state.picUrl" :src="people.$state.picUrl" />
						<div class="user-info-cont">
							<div class="user-info-name">{{ name }}</div>
							<div>{{ role }}</div>
						</div>
					</div>
					<div class="user-info-list">
						当前登录时间：
						<span>{{ date_time }}</span>
					</div>
					<div class="user-info-list">
						当前登录地点：
						<span>{{ address }}</span>
					</div>
				</el-card>
			</el-col>
			<el-col :span="16">
				<el-row :gutter="20" class="mgb20" style="height:  252px; background: #fff; padding: 10px;">
					<el-col :span="8">
						<el-card shadow="hover" :body-style="{ padding: '0px' }">
							<div class="grid-content grid-con-1">
								<el-icon class="grid-con-icon">
									<User />
								</el-icon>
								<div class="grid-cont-right">
									<div class="grid-num">{{ total }}</div>
									<div>用户数量</div>
								</div>
							</div>
						</el-card>
					</el-col>
					<el-col :span="8">
						<el-card shadow="hover" :body-style="{ padding: '0px' }">
							<div class="grid-content grid-con-2">
								<el-icon class="grid-con-icon">
									<ChatDotRound />
								</el-icon>
								<div class="grid-cont-right">
									<div class="grid-num">0</div>
									<div>文章数量</div>
								</div>
							</div>
						</el-card>
					</el-col>
					<el-col :span="8">
						<el-card shadow="hover" :body-style="{ padding: '0px' }">
							<div class="grid-content grid-con-3">
								<el-icon class="grid-con-icon">
									<Goods />
								</el-icon>
								<div class="grid-cont-right">
									<div class="grid-num">0</div>
									<div>签到人数</div>
								</div>
							</div>
						</el-card>
					</el-col>
				</el-row>
				<el-card shadow="hover" style="height: 403px" v-if="false">
					<template #header>
						<div class="clearfix">
							<span>待办事项</span>
							<el-button style="float: right; padding: 3px 0" text>添加</el-button>
						</div>
					</template>

					<el-table :show-header="false" :data="todoList" style="width: 100%">
						<el-table-column width="40">
							<template #default="scope">
								<el-checkbox v-model="scope.row.status"></el-checkbox>
							</template>
						</el-table-column>
						<el-table-column>
							<template #default="scope">
								<div class="todo-item" :class="{
									'todo-item-del': scope.row.status
								}">
									{{ scope.row.title }}
								</div>
							</template>
						</el-table-column>
					</el-table>
				</el-card>
			</el-col>
		</el-row>
		<el-row :gutter="20">
			<!-- :span="24" 代表独占一行-->
			<el-col :span="24">
				<div class="schart-box">
					<!-- <div class="content-title">饼状图</div> -->
					<schart class="schart" canvasId="pie" :options="options3"></schart>
				</div>
			</el-col>
		</el-row>
	</div>
</template>

<script setup lang="ts" name="dashboard">
import Schart from 'vue-schart';
import { reactive ,ref } from 'vue';
import { usePeopleStore } from '../store/people';
import { getCurrentLocation, getUserCount} from '../api/index'

// 把程序所需的数据记录在变量中
let people = usePeopleStore();
let date_time = ref('');
let address = ref('');

const name = localStorage.getItem('ms_username');
const role: string = name === 'admin' ? '超级管理员' : '管理员';

// 配置饼状图
const options3 = reactive({
	type: 'pie',// 表示饼状图
	title: {
		text: '用户群体分析'
	},
	legend: {
		position: 'bottom'
	},
	bgColor: '#fbfbfb',
	labels: ['男生', '女生', '未知性别'],
	datasets: [
		{
			data: [0, 0, 0]
		}
	],
	colorList: ['yellowgreen','deepskyblue','deeppink']
})

const todoList = reactive([
	{
		title: '编写精美的项目界面',
		status: false
	},
	{
		title: '项目数据是动态的',
		status: false
	},
	{
		title: '学会独立解决bug',
		status: false
	},
	{
		title: '掌握整站的开发流程',
		status: false
	},
]);

// 获取定位
getCurrentLocation()
.then(
	(res: any)=>{
		// console.log('x.0',res);
		if(res.code == 200){
			// 设置地址 ?. 可选属性
			address.value = res?.data?.content?.address;
		}
		else {
			address.value = "未知地址"
		}
	}
)

let date = new Date();
date_time.value = date.toLocaleDateString()+" " + date.toLocaleTimeString();


// 统计用户数量
let total = ref(0)
getUserCount({user_sex:'男'}).then(
	(content: any)=>{
		// console.log('2.0',content.result[0])
		options3.datasets[0].data[0] = content.result[0].total;
		total.value += content.result[0].total;
	}
)

getUserCount({user_sex:'女'}).then(
	(content: any)=>{
		// console.log('3.0',content.result[0])
		options3.datasets[0].data[1] = content.result[0].total;
		total.value += content.result[0].total;
	}
)

getUserCount({user_sex:'未知'}).then(
	(content: any)=>{
		// console.log('4.0',content.result[0])
		options3.datasets[0].data[2] = content.result[0].total;
		total.value += content.result[0].total;
	}
)


</script>

<style scoped>
.el-row {
	margin-bottom: 20px;
}

.grid-content {
	display: flex;
	align-items: center;
	height: 100px;
}

.grid-cont-right {
	flex: 1;
	text-align: center;
	font-size: 14px;
	color: #999;
}

.grid-num {
	font-size: 30px;
	font-weight: bold;
}

.grid-con-icon {
	font-size: 50px;
	width: 100px;
	height: 100px;
	text-align: center;
	line-height: 100px;
	color: #fff;
}

.grid-con-1 .grid-con-icon {
	background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
	color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
	background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
	color: rgb(100, 213, 114);
}

.grid-con-3 .grid-con-icon {
	background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
	color: rgb(242, 94, 67);
}

.user-info {
	display: flex;
	align-items: center;
	padding-bottom: 20px;
	border-bottom: 2px solid #ccc;
	margin-bottom: 20px;
}

.user-info-cont {
	padding-left: 50px;
	flex: 1;
	font-size: 14px;
	color: #999;
}

.user-info-cont div:first-child {
	font-size: 30px;
	color: #222;
}

.user-info-list {
	font-size: 14px;
	color: #999;
	line-height: 25px;
}

.user-info-list span {
	margin-left: 70px;
}

.mgb20 {
	margin-bottom: 20px;
}

.todo-item {
	font-size: 14px;
}

.todo-item-del {
	text-decoration: line-through;
	color: #999;
}

.schart {
	width: 100%;
	height: 300px;
}</style>
